package com.op.crm.service.impl;

import java.util.List;

import com.op.crm.persist.dao.SecurityDao;
import com.op.crm.persist.model.Action;
import com.op.crm.persist.model.Group;
import com.op.crm.persist.model.Role;
import com.op.crm.persist.model.User;
import com.op.crm.service.SecurityService;

public class SecurityServiceImpl implements SecurityService{
	
	//违反唯一键约束
	public static final int ORA_00001 = -1;
	public static final int ORA_SUCCESS = 0;

	private SecurityDao securityDao = null;

	public SecurityDao getSecurityDao() {
		return securityDao;
	}

	public void setSecurityDao(SecurityDao securityDao) {
		this.securityDao = securityDao;
	}

	@Override
	public User getUser(String username) throws Exception {
		return securityDao.getUser(username);
	}

	@Override
	public void saveUser(User user) throws Exception {
		securityDao.saveUser(user);
	}

	/* (non-Javadoc)
	 * @see com.nfolok.com.NfkCRM.service.SecurityService#saveAction(com.nfolok.com.NfkCRM.persist.model.Action)
	 * 返回-1代表唯一键冲突，返回0表示插入成功
	 */
	@Override
	public int saveAction(Action action) throws Exception {
		Action exsitsObj = securityDao.getAction(action.getActionId());
		
		if (exsitsObj != null) {
			return ORA_00001;
		}
		
		securityDao.saveAction(action);
		
		return ORA_SUCCESS;
	}

	@Override
	public int saveGroup(Group group) throws Exception {
		Group exsitsObj = securityDao.getGroup(group.getGroupId());
		
		if (exsitsObj != null) {
			return ORA_00001;
		}
		
		securityDao.saveGroup(group);
		
		return ORA_SUCCESS;
	}

	@Override
	public int saveRole(Role role) throws Exception {
		Role exsitsObj = securityDao.getRole(role.getRoleId());
		
		if (exsitsObj != null) {
			return ORA_00001;
		}
		
		securityDao.saveRole(role);
		
		return ORA_SUCCESS;
	}

	@Override
	public Action getAction(String actionId) throws Exception {
		return securityDao.getAction(actionId);
	}

	@Override
	public Role getRole(String roleId) throws Exception {
		return securityDao.getRole(roleId);
	}

	@Override
	public Group getGroup(String groupId) throws Exception {
		return securityDao.getGroup(groupId);
	}

	@Override
	public List<Action> selectActions() throws Exception {
		return securityDao.selectActions();
	}

	@Override
	public List<Action> selectActionByIds(List<String> idList) throws Exception {
		return securityDao.selectActionByIds(idList);
	}

	@Override
	public List<Group> selectGroupByIds(List<String> idList) throws Exception {
		return securityDao.selectGroupByIds(idList);
	}

	@Override
	public List<Role> selectRoleByIds(List<String> idList) throws Exception {
		return securityDao.selectRoleByIds(idList);
	}

	@Override
	public List<User> selectUserByIds(List<String> idList) throws Exception {
		return securityDao.selectUserByIds(idList);
	}

	@Override
	public List<Role> selectRoles() throws Exception {
		return securityDao.selectRoles();
	}

	@Override
	public int updateAction(Action action) throws Exception {
		securityDao.updateAction(action);
		return ORA_SUCCESS;
	}

	@Override
	public int updateGroup(Group group) throws Exception {
		securityDao.updateGroup(group);
		return ORA_SUCCESS;
	}

	@Override
	public int updateRole(Role role) throws Exception {
		securityDao.updateRole(role);
		return ORA_SUCCESS;
	}

	@Override
	public List<Action> selectActionNotIn(List<Action> actions) throws Exception {
		return securityDao.selectActionNotIn(actions);
	}

	@Override
	public void deleteAction(String actionId) throws Exception {
		Action action = securityDao.getAction(actionId);
		if (action != null)
			securityDao.deleteAction(action);
	}
	
	
	
}
